import pandas as pd

# 读取两个Excel文件
file1_path = r'D:\xlzx\能耗\11月试导入.xlsx'
file2_path = r'D:\xlzx\能耗\处理后的报账125.xlsx'

df1 = pd.read_excel(file1_path)
df2 = pd.read_excel(file2_path)

# 创建一个字典来存储KH_CODE和对应的报账电表编码是否与站址编码相同的信息
match_dict = {}

for index, row in df1.iterrows():
    kh_code = row['KH_CODE']
    zhanzhi_bianma = row['站址编码']
    
    # 找到所有KH_CODE匹配的行
    matched_rows = df2[df2['缴费电表编码'] == kh_code]
    
    for _, matched_row in matched_rows.iterrows():
        baozhang_bianma = matched_row['报账电表编码']
        if baozhang_bianma == zhanzhi_bianma:
            match_dict[index] = '是'
            break
    else:
        match_dict[index] = '否'

# 在原始数据框中添加新的列来存储结果
df1['匹配结果'] = df1.index.map(match_dict)

# 将结果保存回Excel文件
output_file_path = r'D:\xlzx\能耗\11月试导入_处理后2.xlsx'
df1.to_excel(output_file_path, index=False)

print(f"处理完成，结果已保存到 {output_file_path}")